Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[input] Added controller support #518

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Big-Smarty
Copy link
Collaborator

Added controller support.
Abstracted input by adding the Input class, overarching both KeyboardMouseInputData and the also newly added GamepadInputData.

@IAmNotHanni IAmNotHanni self-requested a review December 29, 2022 18:10
@IAmNotHanni IAmNotHanni added the feat:input keybord/mouse input label Dec 29, 2022
@IAmNotHanni
Copy link
Member

Nice work! I think I will buy myself a controller just to check if it's working. Will review later

Copy link
Member

@IceflowRE IceflowRE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +84 to +89
[[nodiscard]] glm::ivec2 get_cursor_pos() const;

/// @brief Calculate the change in x- and y-position of the cursor.
/// @return a std::array of size 2 which contains the change in x-position in index 0 and the change in y-position
/// in index 1
[[nodiscard]] std::array<double, 2> calculate_cursor_position_delta();
[[nodiscard]] glm::dvec2 calculate_cursor_position_delta();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use glm types now everywhere or do we abstract them away to keep independent?
@IAmNotHanni @yeetari

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using glm here is ok.

@@ -0,0 +1,212 @@
[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file shouldn't be here.

Comment on lines +3 to +12
#include "glm/detail/qualifier.hpp"
#include <GLFW/glfw3.h>
#include <vector>

#define GLM_PRECISION_HIGHP_DOUBLE
#define GLM_PRECISION_HIGHP_INT
#include <glm/glm.hpp>

#include <array>
#include <shared_mutex>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "glm/detail/qualifier.hpp"
#include <GLFW/glfw3.h>
#include <vector>
#define GLM_PRECISION_HIGHP_DOUBLE
#define GLM_PRECISION_HIGHP_INT
#include <glm/glm.hpp>
#include <array>
#include <shared_mutex>
#include <glm/detail/qualifier.hpp>
#include <GLFW/glfw3.h>
#define GLM_PRECISION_HIGHP_DOUBLE
#define GLM_PRECISION_HIGHP_INT
#include <glm/glm.hpp>
#include <array>
#include <shared_mutex>
#include <vector>

Copy link
Member

@IAmNotHanni IAmNotHanni Dec 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +3 to +6
#include <GLFW/glfw3.h>

#include "inexor/vulkan-renderer/input/gamepad_data.hpp"
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <GLFW/glfw3.h>
#include "inexor/vulkan-renderer/input/gamepad_data.hpp"
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp"
#include "inexor/vulkan-renderer/input/gamepad_data.hpp"
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp"
#include <GLFW/glfw3.h>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 12 to +14
vulkan-renderer/input/keyboard_mouse_data.cpp
vulkan-renderer/input/gamepad_data.cpp
vulkan-renderer/input/input.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
vulkan-renderer/input/keyboard_mouse_data.cpp
vulkan-renderer/input/gamepad_data.cpp
vulkan-renderer/input/input.cpp
vulkan-renderer/input/gamepad_data.cpp
vulkan-renderer/input/input.cpp
vulkan-renderer/input/keyboard_mouse_data.cpp

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes: keep in alphabetical order

Comment on lines +1 to +2
#include <inexor/vulkan-renderer/input/gamepad_data.hpp>
#include <mutex>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <inexor/vulkan-renderer/input/gamepad_data.hpp>
#include <mutex>
#include "inexor/vulkan-renderer/input/gamepad_data.hpp"
#include <mutex>

Comment on lines +1 to +3
#include "GLFW/glfw3.h"
#include "spdlog/spdlog.h"
#include <inexor/vulkan-renderer/input/input.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "GLFW/glfw3.h"
#include "spdlog/spdlog.h"
#include <inexor/vulkan-renderer/input/input.hpp>
#include "inexor/vulkan-renderer/input/input.hpp"
#include <GLFW/glfw3.h>
#include <spdlog/spdlog.h>

#include <inexor/vulkan-renderer/input/input.hpp>

namespace inexor::vulkan_renderer::input {
void Input::key_callback(GLFWwindow * /*window*/, int key, int, int action, int /*mods*/) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void Input::key_callback(GLFWwindow * /*window*/, int key, int, int action, int /*mods*/) {
void Input::key_callback(GLFWwindow * /*window*/, int key, int /*scancode*/, int action, int /*mods*/) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes: That's the standard way of saying "there once was a parameter" and also to mention the name of the parameter.

Comment on lines 1 to +3
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp"
#include "GLFW/glfw3.h"
#include "glm/fwd.hpp"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp"
#include "GLFW/glfw3.h"
#include "glm/fwd.hpp"
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp"
#include <GLFW/glfw3.h>
#include <glm/fwd.hpp>

Comment on lines 1 to 4
#include "inexor/vulkan-renderer/wrapper/window.hpp"
#include "GLFW/glfw3.h"

#include <spdlog/spdlog.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "inexor/vulkan-renderer/wrapper/window.hpp"
#include "GLFW/glfw3.h"
#include <spdlog/spdlog.h>
#include "inexor/vulkan-renderer/wrapper/window.hpp"
#include <GLFW/glfw3.h>
#include <spdlog/spdlog.h>

@IceflowRE
Copy link
Member

Other things:
Can you rename the commits that they match our naming convention? See the Github Actions.
Ignore the failed building for now, it is broken, will see when i get time for that.

This will only support one Gamepad right? not multiple?

@IAmNotHanni
Copy link
Member

@IceflowRE About multiple gamepads/joysticks: I just ordered several online. We should do it in another pull request pls.

@IAmNotHanni
Copy link
Member

IAmNotHanni commented Dec 30, 2022

  • Update docs

https://inexor-vulkan-renderer.readthedocs.io/en/latest/development/reference/keyboard-mouse-input.html
The reference page about input in the docs states:

Inexor does not support joysticks yet.

That's no longer true. Also, the ref pages needs to be renamed to "keyboard, mouse and joystick input"
Furthermore, the docs about keyboard and mouse are now outdated as well.
I will take care of this. I will push into this pull request then.

Copy link
Member

@yeetari yeetari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, code looks good, please could you squash the commits though.

Comment on lines +7 to +8
#define GLM_PRECISION_HIGHP_DOUBLE
#define GLM_PRECISION_HIGHP_INT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define GLM_PRECISION_HIGHP_DOUBLE
#define GLM_PRECISION_HIGHP_INT

These don't do anything.


if (m_camera->type() == CameraType::LOOK_AT && m_input_data->is_mouse_button_pressed(GLFW_MOUSE_BUTTON_LEFT)) {
auto deadzone_lambda = [](const float state) {
if (state > -0.2f && state < 0.2f) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How were these constants chosen? Also it might be easier written as glm::abs(state) < 0.2f (at least for me it looks pretty confusing at first)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the constants should be named as static constexpr WHAT_IS_MY_NAME{0.2f};

m_camera->set_movement_state(CameraMovement::LEFT, m_input_data->is_key_pressed(GLFW_KEY_A));
m_camera->set_movement_state(CameraMovement::BACKWARD, m_input_data->is_key_pressed(GLFW_KEY_S));
m_camera->set_movement_state(CameraMovement::RIGHT, m_input_data->is_key_pressed(GLFW_KEY_D));
if (m_camera->type() == CameraType::LOOK_AT) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for now, though in future the Input class should abstract away whether the input event came from kb&m or controller, with a mapping system (so both a controller and mouse could be mapped to a "vertical" and "horizontal" axis that the application can query from m_input).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah in the future I want to refactor the camera code anyways (see #474)

private:
std::array<glm::vec2, 2> m_current_joystick_axes{};
std::array<glm::vec2, 2> m_previous_joystick_axes{};
std::array<std::array<bool, GLFW_GAMEPAD_BUTTON_LAST + 1>, GLFW_GAMEPAD_BUTTON_LAST> m_button_states{};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the outer array size should be GLFW_JOYSTICK_LAST + 1?

}

void Input::update_gamepad_data() {
if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1) == 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could early return here to make it a little easier to read.

@IAmNotHanni IAmNotHanni added the cat:enhancement enhancement/requested feature/update of existing features label May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat:enhancement enhancement/requested feature/update of existing features feat:input keybord/mouse input
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants